<script>
import styled from 'vue-styled-components'
import req from '@/api/apiSetting'
const StyledContainer = styled.div`
  width: 100%;
  height: 100%;
  display:flex;
  align-items:center;
  justify-content:center;

  .el-loading-spinner {
    .path{
      stroke:#0F9D7D;
    }

    .el-loading-text{
      color:#0F9D7D;
    }
  }
`
const MAX_CHECK_COUNT = 10
export default {
  data () {
    return {
      hasError: false,
    }
  },
  created () {
    this.loadHomePage()
  },
  methods: {
    loadHomePage () {
      // check server health and switch to main page
      let checkCount = 0
      let that = this
      setTimeout(function cycle () {
        req.get('api/ping').then(() => {
          that.$router.push('/daily')
        }).catch(() => {
          if (checkCount < MAX_CHECK_COUNT) {
            checkCount++
            setTimeout(cycle, 300)
          } else {
            that.hasError = true
          }
        })
      }, 200)
    },
  },
  render () {
    return (
      <StyledContainer>
        {this.hasError ? (
          <el-result icon="error" title="服务启动失败" subTitle="请关闭重启或联系开发人员" />
        ) : (
          <div style="width:100%;height:100%;" v-loading={true} element-loading-text="服务初始化中"/>
        )}
      </StyledContainer>
    )
  },
}
</script>
